package com.mayisoftware.mayioa.business.flow.impl.serviceImpl;

import com.mayisoftware.mayioa.business.flow.api.domain.BaseFlowInfo;
import com.mayisoftware.mayioa.business.flow.api.service.IFlowBaseService;
import com.mayisoftware.mayioa.business.plan.api.service.IPlanConstraintService;
import com.mayisoftware.mayioa.common.impl.utils.bean.ReflectionUtil;
import com.mayisoftware.mayioa.platform.api.service.ISysUserService;
import com.mayisoftware.mayioa.platform.impl.util.SpringUtils;
import org.flowable.engine.HistoryService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.delegate.JavaDelegate;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.engine.runtime.Execution;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.task.service.delegate.DelegateTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.List;

/**
 * 通用的报名计划检查信息
 */
@Component
public class CheckSystemDelegate implements  JavaDelegate  {


    /**
     * Flowable 流程处理引擎
     */
    @Autowired
    private IFlowBaseService flowBaseService;

    /**
     * 日志类
     */
    private static final Logger log = LoggerFactory.getLogger(CheckSystemDelegate.class);

    @Override
    public void execute(DelegateExecution delegateExecution) {
        log.warn("通用的系统自动报名检查开始执行execute");

        boolean checkOk = true;
        String checkMsg = "";

        try
        {
            // 使用计划服务读取计划配置的限制条件，校验用户的相关信息
            IPlanConstraintService planConstraintService = SpringUtils.getBean(IPlanConstraintService.class);

            if (null == planConstraintService){
                checkOk = false;
                log.error("检验服务不存在");
            }
            else {
                String userId =(String) delegateExecution.getVariable("applyUserId");
                String tmpPlanId =(String)delegateExecution.getVariable("planId");
                checkMsg= planConstraintService.checkPlanIsCanEnter(Long.parseLong(tmpPlanId),Long.parseLong(userId));

                if(checkMsg.equalsIgnoreCase("success")){
                    checkOk=true;
                }
                else {
                    checkOk=false;
                }
            }
        }
        catch (Exception e){
            log.error("系统审核发生异常",e);
            checkOk =false;
        }

        //校验通过后设置outcome
        delegateExecution.setVariable("flowName",delegateExecution.getCurrentFlowElement().getName());

        if(checkOk){
            delegateExecution.setVariable("remark","系统审核通过");
            delegateExecution.setVariable("outcome","通过");
        }
        else {
            delegateExecution.setVariable("remark","系统审核失败,原因:"+checkMsg);
            delegateExecution.setVariable("outcome","驳回");
        }

        Field[] fields = ReflectionUtil.getAllDeclareFields(BaseFlowInfo.class);

        for (Field field:fields){
            Object tmpValue =delegateExecution.getVariable(field.getName());
            if (tmpValue == null && field.getName().equalsIgnoreCase("ApprovalUserId")){
                tmpValue = "system";
            }

            if (tmpValue == null && field.getName().equalsIgnoreCase("ApprovalUserName")){
                tmpValue = "system";
            }

            delegateExecution.setVariable(delegateExecution.getCurrentFlowElement().getId()+"|"+field.getName(),tmpValue);
        }

    }
}
